package itsur.arquitecturas.implicitinvocation;

/**************************************************************************************
** Class name: ListCoursesTaken
** Author: ITSUR
** Date: 2/Dic/08
** Version 1.0
***************************************************************************************
** Purpose: Assignment 3 for SE 17-655, Architectures for Software Systems. This is a
** standalone thread that will list the core courses a student hasn't taken yet.
**
** Abstract: Upon notification, this thread will first list the students and ask the
** user to pick a student and enter the student's ID.  If the student's ID is valid,
** then list courses that the student has already taken..
**
** Modification Log
**************************************************************************************
NOTE: This class is based in the class ListPendingCORECourses.
**************************************************************************************/

import java.util.Observable;

public class ListCoursesTaken extends Communication implements Runnable {
	boolean Stop = false;

	public ListCoursesTaken ( Integer RegistrationNumber, String ComponentName )
	{
		super( RegistrationNumber, ComponentName  );

	} // Constructor

	public void update( Observable Thing, Object NotificationNumber )
	{
		boolean Done, Done2;
		Course CurrentObject = null;
		Menus Menu = new Menus();
		Displays Display = new Displays();
		Student MyStudent = new Student();
		// The update() method is an abstract method that is called whenever the
		// notifyObservers() method is called by the Observable class
		// First we check to see if the NotificationNumber is equal to this
		// thread's RegistrationNumber or SYSTEM_SHUT_DOWN.  If they are, then we
		// execute - otherwise, we stay idle.

		Done = false;
		Done2 = false;
		
		if (RegistrationNumber.compareTo(NotificationNumber) == 0) {
			// First we use a Displays object to list all of the students. Then
			// we
			// ask the user to pick a student using a Menus object.

			AddToReceiverList("ListStudentsComponent");

			SignalReceivers("ListStudentsComponent");

			MyStudent = Menu
					.PickStudent(SystemData.TheListOfStudents.ListOfStudents);

			// If the user selected an invalid student, then a message is
			// printed to the
			// terminal.

			if (MyStudent != null) {

				CourseList CoursesTaken = new CourseList();
				CourseList coreCourses = SystemData.TheListOfCourses.ListOfCourses;
				Course CourseObject = null;
			
				//Busca el nombre de los cursos que el estudiante ya ha tomado 
				MyStudent.CoursesTakenList.GoToFrontOfList();
				while(!Done)
				{
					Done2=false;
					CurrentObject = MyStudent.CoursesTakenList.GetNextCourse();
					if (CurrentObject == null)
					{	
						Done=true;
					}
					else
					{
					
					//Toma la clave de cada uno de los cursos que el alumno ha tomado 
					//y la compara en la lista de cursos existente
					coreCourses.GoToFrontOfList();
					while(!Done2)
					{ 
						CourseObject = coreCourses.GetNextCourse();
						if (CourseObject==null){
							Done2=true;}
						else
						{
						if (CurrentObject.CourseID.compareToIgnoreCase( CourseObject.CourseID)==0 )//Si encontro el curso...
						{
							CurrentObject.CourseName = CourseObject.CourseName;//Asigna el nombre del curso al curso ya tomado.
							Done2=true;
						}
						}
					}
					if(CurrentObject.CourseName==null)
						CurrentObject.CourseName= "Sin Nombre";
					CoursesTaken.AddCourse(CurrentObject );//Agrega el curso con el nombre a una lista de cursos tomados
					}

					
					
				}

				Display.DisplayStudentCoursesTaken(MyStudent, CoursesTaken );
			} else {

				System.out.println("\n\n *** Student not found ***");

			} // if

		} // if
		
		if ( SYSTEM_SHUT_DOWN_ID.compareTo(NotificationNumber) == 0 )
		{
			Stop = true;

		} 

	} // update


   	public void run()
   	{
   		System.out.println( "\nListCoursesTaken Component Started..." );

   		try
   		{
   			while( !Stop )
            {

   				// Note this is something of a farce here.  In order to start each
    			// component as a separate thread, the run() method must be implemented.
                // To illustrate how run() could be used here I put some code here.We really
                // don't do anything here,.. but we could.  If we were doing things here,
                // whatever it was that we were doing would be interrupted when an event
                // from the executive arrived, and the update() method we would immediately
                // execute. Currently we wait forever (by 1 second increments).  Please feel
                // free to put code if if you thing you need to (not to imply that you will
                // need to).

               	Thread.sleep( 1000 );

			} // while

			System.out.println( "\nListCoursesTaken Component Shutdown..." );

		} catch (Exception e) {

			System.out.println( "Exception occured in ListCoursesTaken.run(): " + e);

		}  // try-catch

	} // run
}
